﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualBasic;
using SqlsugarBlog.IService;
using SqlsugarBlog.Model;

namespace SqlsugarBlog.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BlogNewsController : ControllerBase
    {
        private IBlogNewService _blogNewsService;
        public BlogNewsController(IBlogNewService blogNewService) 
        {
            _blogNewsService=blogNewService;
        }
        [HttpGet("QueryAll")]
        public async Task<ActionResult> Query()
        {
            var blognews=await _blogNewsService.QueryAsync();
            ApiResult result = new ApiResult()
            {
                Code = 200,
                Message = "操作成功",
                Data = blognews
            };
            return Ok(result);
        }

        [HttpGet("Query")]
        public async Task<ActionResult> Query(int id)
        {
            var blognews = await _blogNewsService.QueryByIdAsync(id);
            ApiResult result = new ApiResult()
            {
                Code = 200,
                Message = "操作成功",
                Data = blognews
            };
            return Ok(result);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpPost("Add")]
        public async Task<ActionResult<ApiResult>> Add(string title, string content)
        {
            BlogNews blogNews = new BlogNews()
            {
                Title = title,
                Content = content,
                TypeId = 1,
                WriterId = 1,
                Time = DateTime.Now
            };
            bool bRet=  await _blogNewsService.AddAsync(blogNews);
            ApiResult result = new ApiResult();
            result.Data = blogNews;
            if (bRet)
            {
                result.Code = 200;
                result.Message = "添加成功";
            }
            else
            {
                result.Code = 500;
                result.Message = "添加失败";
            }

            return result;
        }
        [HttpPost("Delete")]
        public async Task<ActionResult<ApiResult>> Delete(int id)
        {
            var blogNews=await _blogNewsService.QueryByIdAsync(id);
            ApiResult result = new ApiResult();
            result.Data = blogNews;
            if (blogNews == null)
            {
                result.Code = 500;
                result.Message = $"没有找到Id为{id}的内容";
            }
            else
            {
                bool bRet = await _blogNewsService.DeleteAsync(id);
                if (bRet)
                {
                    result.Code = 200;
                    result.Message = "删除成功";
                }
                else
                {
                    result.Code = 500;
                    result.Message = "删除失败";
                }
            }
            return result;
        }
        [HttpPost("Edit")]
        public async Task<ActionResult<ApiResult>> Edit(int id,string title,string content,int typeId)
        {
            var blogNews =await _blogNewsService.QueryByIdAsync(id);
            ApiResult result = new ApiResult();
            result.Data = blogNews;
            if (blogNews == null)
            {
                result.Code = 500;
                result.Message = $"没有找到Id为{id}的内容";
            }
            else
            {
                blogNews.Title = title;
                blogNews.Content = content;
                blogNews.TypeId = typeId;
                bool bRet = await _blogNewsService.ModifyAsync(blogNews);
                if (bRet)
                {
                    result.Code = 200;
                    result.Message = "修改成功";
                }
                else
                {
                    result.Code = 500;
                    result.Message = "修改失败";
                }
            }

            return result;
        }
    }
}
